lapop12 = readRDS('data/lapop12.rds')

#### Placebo Tests####

#fake treatment
pre_treat <- subset(lapop12, trial == 0)
pre_treat <- subset(pre_treat, day != "26")
pre_treat$placebo.treat <- as.numeric(pre_treat$trend > median(pre_treat$trend), 1,0)


m1 <- lm_robust(torture_bin ~ placebo.treat, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')
m2 <- lm_robust(soc.cl_bin ~ placebo.treat, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')
m3 <- lm_robust(fair_trial ~ placebo.treat, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')

coef.name <- c('Torture', 'Soc. Cleanse', 'Fair Trial')
y <- c(summary(m1)$coefficients[,1], summary(m2)$coefficients[,1],
       summary(m3)$coefficients[,1])  
U <- c(summary(m1)$coefficients[,6], summary(m2)$coefficients[,6], 
       summary(m3)$coefficients[,6])  
L <- c(summary(m1)$coefficients[,5], summary(m2)$coefficients[,5],  
       summary(m3)$coefficients[,5])  
pv <- c(m1$p.value, m2$p.value,  
        m3$p.value)
plot.df <- data.frame(coef.name, y, U, L, pv)
plot.df$y2 <- round(plot.df$y, 2)
plot.df$y2 <- ifelse(plot.df$pv < .05 & plot.df$pv > .01, paste0(plot.df$y2, "*"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .01 & plot.df$pv > .001, paste0(plot.df$y2, "**"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .001, paste0(plot.df$y2, "***"), plot.df$y2)



p1 <- ggplot(plot.df, aes(x = coef.name, y = y)) +
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = U, ymin = L), width=.1,
                position=position_dodge(.9)) + 
  geom_hline(yintercept = 0) + 
  ggtitle("Placebo Check: Fake Treatment") + 
  xlab("Outcomes") +
  ylab("Partial Derivative") +
  annotate("label", x = coef.name, y = y, label = plot.df$y2, 
           hjust=.5, vjust=1, family="Times") +
  ylim(-.75, .75) + 
  theme_tufte()

p1

#pretrends

m1 <- lm_robust(torture_bin ~ trend, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')


m2 <- lm_robust(soc.cl_bin ~ trend, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')

m3 <- lm_robust(fair_trial ~ trend, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')




coef.name <- c('Torture', 'Cleanse', 'Fair Trial')
y <- c(summary(m1)$coefficients[,1], summary(m2)$coefficients[,1],
       summary(m3)$coefficients[,1])  
U <- c(summary(m1)$coefficients[,6], summary(m2)$coefficients[,6], 
       summary(m3)$coefficients[,6])  
L <- c(summary(m1)$coefficients[,5], summary(m2)$coefficients[,5],  
       summary(m3)$coefficients[,5])  
pv <- c(m1$p.value, m2$p.value,  
        m3$p.value)
plot.df <- data.frame(coef.name, y, U, L, pv)
plot.df$y2 <- round(plot.df$y, 2)
plot.df$y2 <- ifelse(plot.df$pv < .05 & plot.df$pv > .01, paste0(plot.df$y2, "*"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .01 & plot.df$pv > .001, paste0(plot.df$y2, "**"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .001, paste0(plot.df$y2, "***"), plot.df$y2)


p2 <- ggplot(plot.df, aes(x = coef.name, y = y)) +
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = U, ymin = L), width=.1,
                position=position_dodge(.9)) + 
  geom_hline(yintercept = 0) + 
  ggtitle("Placebo Check: Pretrends") + 
  annotate("label", x = coef.name, y = y, label = plot.df$y2, 
           hjust=.5, vjust=1, family="Times") +
  xlab("Outcomes") +
  ylab("Partial Derivative") +
  ylim(-.25, .25) + 
  theme_tufte()

# coup anniversary

pre_treat$coup.event <- as.numeric(pre_treat$day %in% c("24","25"), 1,0)


m1 <- lm_robust(torture_bin ~ coup.event, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')
m2 <- lm_robust(soc.cl_bin ~ coup.event, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')
m3 <- lm_robust(fair_trial ~ coup.event, data = pre_treat, 
                fixed_effects = estratopri + estratosec + ur, 
                se_type = 'stata')



coef.name <- c('Torture', 'Cleanse', 'Fair Trial')
y <- c(summary(m1)$coefficients[,1], summary(m2)$coefficients[,1],
       summary(m3)$coefficients[,1])  
U <- c(summary(m1)$coefficients[,6], summary(m2)$coefficients[,6], 
       summary(m3)$coefficients[,6])  
L <- c(summary(m1)$coefficients[,5], summary(m2)$coefficients[,5],  
       summary(m3)$coefficients[,5])  
pv <- c(m1$p.value, m2$p.value,  
        m3$p.value)
plot.df <- data.frame(coef.name, y, U, L, pv)
plot.df$y2 <- round(plot.df$y, 2)
plot.df$y2 <- ifelse(plot.df$pv < .05 & plot.df$pv > .01, paste0(plot.df$y2, "*"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .01 & plot.df$pv > .001, paste0(plot.df$y2, "**"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .001, paste0(plot.df$y2, "***"), plot.df$y2)


p3 <- ggplot(plot.df, aes(x = coef.name, y = y)) +
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = U, ymin = L), width=.1,
                position=position_dodge(.9)) + 
  geom_hline(yintercept = 0) + 
  ggtitle("Placebo Check: Coup Anniversary") + 
  annotate("label", x = coef.name, y = y, label = plot.df$y2, 
           hjust=.5, vjust=1, family="Times") +
  xlab("Outcomes") +
  ylab("Partial Derivative") +
  theme_tufte()


plot_grid(p1, p2, p3, labels = "AUTO", ncol = 3)
ggsave("fig-out/placebo_exclusion_checks.pdf", width = 15)
